﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MoneyManagerNET.Model;

namespace MoneyManagerNET.Presentation.ViewModel
{
    public class SummaryOfAccountsReportViewModel : ReportViewModelBase
    {
        public string UserName { get { return "Tural"; } }
        public String Today { get { return DateTime.Today.ToString(); } }
        public AccountSummaryGroup RootGroup { get; set; }

        public override void Execute()
        {
            base.Execute();
            List<AccountSummary> list = new List<AccountSummary>();
            var accountQuery=
                from account in App.Storage.Select<IBankAccount>() 
                select new AccountSummary(account.Name,account.Balance);
            
            list.Add( new AccountSummaryGroup("Banl Account Total : ", accountQuery.ToArray()));
            list.Add(new AccountSummary("Stock Investments",App.Storage.Select<IStockInvestment>().Sum(inv=>inv.CurrentPrice)));
            list.Add(new AccountSummary("Assets", App.Storage.Select<IAsset>().Sum(ass => ass.GetCurrentValue())));

            RootGroup = new AccountSummaryGroup("Total Balance on all Accounts",list.ToArray());

        }

        public SummaryOfAccountsReportViewModel()
        {
            Header = "Summary Of Accounts";
        }
    }

    #region display logic

    public class AccountSummary
    {
        public string Name { get; protected set; }
        public decimal Balance { get; protected set; }
        public AccountSummary(string name, decimal balance)
        {
            this.Name = name;
            this.Balance = balance;
        }
        public virtual int Depth {get{return 1;}}
    }

    public class AccountSummaryGroup : AccountSummary
    {
        public AccountSummary[] Children { get;  set; }
        public AccountSummaryGroup(string name, AccountSummary[] Children)
            : base(name, Children.Sum(i => i.Balance))
        {
            this.Children = Children;
        }
        public override int Depth
        {
            get
            {
                var max=Children.Max(i=>i.Depth);
                max++;
                return max;
            }
        }
    }

  
    #endregion

    
}
